/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookshop.dao;

import com.bookshop.entity.Author;
import com.bookshop.entity.Book;
import com.bookshop.entity.Category;
import com.bookshop.entity.Collection;
import com.bookshop.entity.Publisher;
import com.bookshop.entity.Serie;
import com.bookshop.entity.Type;
import com.bookshop.exception.EBookshopException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Singleton;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Thibault
 */
@Singleton(mappedName = "com.bookshop.dao.book")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class DaoBookImpl implements IDaoBookLocal, IDaoBookRemote, Serializable {

    @PersistenceContext
    private EntityManager em; 
    
    @Override
    public List<Book> getAllBooks() {
        try {
            return em.createNamedQuery("Book.findAll").getResultList();
        } catch(Throwable th) {
            throw new EBookshopException(th.getMessage(), th);
        }
    }

    @Override
    public List<Book> getAllBooksByCategoryId(int categoryId) {
        try {
            return em.createNamedQuery("Book.findByCategoryId").setParameter("categoryId", new Category(categoryId)).getResultList();
        } catch(Throwable th) {
            throw new EBookshopException(th.getMessage(), th);
        }
    }

    @Override
    public Book getBookByISBN(String isbn) {
        try {
            return (Book) em.createNamedQuery("Book.findByIsbn").setParameter("isbn", isbn).getSingleResult();
        } catch (Throwable th) {
            throw new EBookshopException(th.getMessage(), th);
        }
    }

    @Override
    public List<Book> getAllBooksByTitle(String title) {
        try {
            return em.createNamedQuery("Book.findByTitle").setParameter("title", "%"+title+"%").getResultList();
        } catch(Throwable th) {
            throw new EBookshopException(th.getMessage(), th);
        }
    }

    @Override
    public List<Book> getAllBooksBySerie(Serie serie) {
        try {
            return em.createNamedQuery("Book.findBySerie").setParameter("serie", serie).getResultList();
        } catch(Throwable th) {
            throw new EBookshopException(th.getMessage(), th);
        }    
    }

    @Override
    public List<Book> getAllBooksByAuthor(Author author) {
        try {
            return em.createNamedQuery("Book.findByAuthor").setParameter("author", author).getResultList();
        } catch(Throwable th) {
            throw new EBookshopException(th.getMessage(), th);
        }
    }

    @Override
    public List<Book> getAllBooksByCollection(Collection collection) {
        try {
            return em.createNamedQuery("Book.findByCollection").setParameter("collection", collection).getResultList();
        } catch(Throwable th) {
            throw new EBookshopException(th.getMessage(), th);
        }
    }

    @Override
    public List<Book> getAllBooksByPublisher(Publisher publisher) {
        try {
            return em.createNamedQuery("Book.findByPublisher").setParameter("publisher", publisher).getResultList();
        } catch(Throwable th) {
            throw new EBookshopException(th.getMessage(), th);
        }
    }

    @Override
    public List<Book> getAllBooksByType(Type type) {
        try {
            return em.createNamedQuery("Book.findByType").setParameter("type", type).getResultList();
        } catch(Throwable th) {
            throw new EBookshopException(th.getMessage(), th);
        }
    }

    @Override
    public List<Book> getAllBooksByCategory(Category category) {
        try {
            return em.createNamedQuery("Book.findByCategory").setParameter("category", category).getResultList();
        } catch(Throwable th) {
            throw new EBookshopException(th.getMessage(), th);
        }    }
    
    
    
}
